package com.springcloud.sc.ucpv5.client.application.message;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.springcloud.sc.ucpv5.client.commondata.data.*;
import org.omg.CORBA.portable.Streamable;
import com.springcloud.sc.ucpv5.client.application.util.Constant;
import com.springcloud.sc.ucpv5.client.common.CmdDefine;
import com.springcloud.sc.ucpv5.client.commondata.BaseListModel;
import com.springcloud.sc.ucpv5.client.commondata.IBaseListModel;
import com.springcloud.sc.ucpv5.client.commondata.ModelBase;
import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.commondata.MyUserInfoData;
import com.springcloud.sc.ucpv5.client.commondata.SecuResult;
import com.springcloud.sc.ucpv5.client.commondata.datachange.DataExChange;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager;
import com.springcloud.sc.ucpv5.client.util.MD5Util;
import com.springcloud.sc.ucpv5.client.util.PublicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataMessageManager {

	private static final Logger logger = LoggerFactory.getLogger(DataMessageManager.class);

	private static DataMessageManager instance;
	private static final int DEFAULT_TIMEOUT = 10 * 1000;

	private MessageService msgService;

	public static DataMessageManager getInstance() {
		if (instance == null) {
			instance = new DataMessageManager();
		}

		return instance;
	}

	private DataMessageManager() {
		msgService = MessageService.getInstance();
	}

	private SecuResult dcsmain_comm_msg(String physicsNodeNumber, String loggicNodeNumber, Object operObj, int cmdId, DataExChange req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		SecuResult result = new SecuResult();
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
		int retCode = msgService.sendSyncMsg(header, req, rsp, isAutoBusy, DEFAULT_TIMEOUT);

		if (retCode == 0) {
			result.addResult(operObj, 0, cmdId, rsp.Result, isWriteLog);
		} else {
			result.addResult(operObj, 0, cmdId, retCode, isWriteLog);
		}
		return result;
	}

	public int initMessageService(String ip, int port) {
		try {
			return MessageService.getInstance().init(ip, port);
		} catch (Throwable e) {
			logger.info("DataMessageManager initMessageService  " + ip + " " + port, e);
			return -1;
		}
	}

	private String loginUserName = null;

	public LoginRsp LoginSystem(String name, String password, SecuResult result, boolean isAutoBusy, boolean isWriteLog) {
		LoginReq req = new LoginReq();
		req.UserName = name;
		req.UserPwd = password;
		// req.value.LocalIp = MessageService.getInstance().getLocalIp();

		LoginRsp rsp = new LoginRsp();

		int loginCmdCode = CmdDefine.CMD_User_Login;
		// wsy 20160912 V4版本, 登陆的命令码都采用新的命令码进行登陆
		// 表示支持调度台版本校验的
		// wsy 20180102 新增登录时md5校验支持
	/*	boolean 登录Md5校验支持 = false;
		if (SettingManager.SYS.ServerVerType_V5()) {
			登录Md5校验支持 = true;
		}*/
		if (true) {
			loginCmdCode = CmdDefine.CMD_User_Login_md5;
			req.UserName = name;
			req.UserPwd = "";

			MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, loginCmdCode, null, null);
			int retCode = msgService.sendSyncMsg(header, req, rsp, isAutoBusy, 2 * 1000);

			if (retCode != 0) {
				System.out.println("同步返回失败 !!!");
				rsp.Result = retCode;
				return rsp;
			}
			// 利用返回值和密码做md5处理
			try {
				String newPsw = MD5Util.getFileMD5String(password + (rsp.Auth_NodeID_List + ""));
				password = newPsw;
			} catch (Exception e) {
//				PublicUtil.debugInfo("uap 鉴权消息 失败:" + e.getMessage());
			}
			loginCmdCode = CmdDefine.CMD_User_Login_md5_check;

			req.UserName = name;
			req.UserPwd = password;
		} else {
			loginCmdCode = CmdDefine.CMD_User_Login_regcheck;
		}

		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, loginCmdCode, null, null);
		int retCode = msgService.sendSyncMsg(header, req, rsp, isAutoBusy, DEFAULT_TIMEOUT);

		if (retCode == 0) {
			result.addResult(name, 0, loginCmdCode, rsp.Result, isWriteLog);
			if (rsp.Result == 0) {
				this.loginUserName = name;

				// 握手线程处理
				msgService.shakeHandCheck();
			}
		} else if (retCode == 6033) {
			if (true) {
				CommonRsp rsp_ = new CommonRsp();
				CommonReq req_ = new CommonReq();
				req.UserName = name;
				dcsmain_comm_msg(null, null, req_.UserName, CmdDefine.CMD_User_Logout, req_, rsp_, false, false);
			}

			header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, loginCmdCode, null, null);
			retCode = msgService.sendSyncMsg(header, req, rsp, isAutoBusy, DEFAULT_TIMEOUT);
			if (retCode == 0) {
				result.addResult(name, 0, loginCmdCode, rsp.Result, isWriteLog);
				if (rsp.Result == 0)
					this.loginUserName = name;
			}
		} else {
			result.addResult(name, 0, loginCmdCode, retCode, isWriteLog);
		}

		return rsp;
	}

	public CommonRsp LogoutSystem(SecuResult result, boolean isAutoBusy, boolean isWriteLog) {
		CommonRsp rsp = new CommonRsp();
		if (loginUserName == null)
			return rsp;
		CommonReq req = new CommonReq();
		req.UserName = loginUserName;

		/*if (SettingManager.SYS.ServerVerType_V5()) {
			return rsp;
		}*/
		if(true){
			return rsp;
		}
		SecuResult tmpResult = dcsmain_comm_msg(null, null, req.UserName, CmdDefine.CMD_User_Logout, req, rsp, isAutoBusy, isWriteLog);
		result.uniteResult(tmpResult);
		return rsp;
	}

	public SecuResult nodeAdd(String physicsNodeNumber, String loggicNodeNumber, Node req, NodeCommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_NODE_ADD;
		Object operObj = req.NodeName;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, null);

		int retCode = msgService.sendSyncMsg(header, req, rsp, isAutoBusy, DEFAULT_TIMEOUT);
//		System.out.println("增加节点 节点:" + req.ParentNumber + " NodeNumber:" + req.NodeNumber + " NodeName:" + req.NodeName);

		if (retCode == 0) {
			result.addResult(operObj, 0, cmdId, rsp.Result, isWriteLog);
		} else {
			result.addResult(operObj, 0, cmdId, retCode, isWriteLog);
		}
		return result;
	}

	public SecuResult nodeDel(String physicsNodeNumber, String loggicNodeNumber, Node req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
	try {
	    // wsy 20190104 兼容V4 上传旧的节点必须先删除节点管理员
	    User item = new User();
	    item.UserTelno = "F" + req.NodeNumber;
	    SecuResult result = userDel(req.Physics_NodeNumber, req.NodeNumber, item, new CommonRsp(), true, true);
	} catch (Exception e) {
	    // TODO: handle exception
	}
//	System.out.println("删除节点 节点:" + req.ParentNumber + " " + req.NodeNumber + " " + req.NodeName);
	return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.NodeName, CmdDefine.CMD_NODE_Del, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult nodeModify(String physicsNodeNumber, String loggicNodeNumber, Node req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {

//		System.out.println("修改节点 节点:" + req.ParentNumber + " " + req.NodeNumber + " " + req.NodeName);

		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.NodeName, CmdDefine.CMD_NODE_Modify, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult groupModify(String physicsNodeNumber, String loggicNodeNumber, Group req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.getGroupName(), CmdDefine.CMD_Group_Modify, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult nodeQueryNodeList(String physicsNodeNumber, String loggicNodeNumber, NodeQueryRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_NODE_Query_List;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		int retCode = msgService.sendSyncMsg(header, new CommonReq(), rsp, isAutoBusy, DEFAULT_TIMEOUT);

		if (retCode == 0) {
	    result.addResult(loggicNodeNumber, 0, cmdId, rsp.Result, isWriteLog);
		} else {
	    result.addResult(loggicNodeNumber, 0, cmdId, retCode, isWriteLog);
		}
		return result;
	}

	public SecuResult groupAdd(String physicsNodeNumber, String loggicNodeNumber, Group req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.GroupName, CmdDefine.CMD_Group_Add, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult groupMod(String physicsNodeNumber, String loggicNodeNumber, Group req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.GroupName, CmdDefine.CMD_Group_Modify, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult groupDel(String physicsNodeNumber, String loggicNodeNumber, Group req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.GroupName, CmdDefine.CMD_Group_Del, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult groupJoin_Add(String physicsNodeNumber, String loggicNodeNumber, Group req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.GroupName, CmdDefine.CMD_Join_Group_Group_Add, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult groupJoin_Del(String physicsNodeNumber, String loggicNodeNumber, Group req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.GroupName, CmdDefine.CMD_Join_Group_Group_Del, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userAdd(String physicsNodeNumber, String loggicNodeNumber, UserInfo req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Add, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userAdd(String physicsNodeNumber, String loggicNodeNumber, MyUserInfoData req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		try {
			int userType = req.userInfo.UserType;
			int keep2 = Integer.valueOf(req.组播类型);

			// 如果等于1的话, 将第二个字节的第二位附上keep2的值
			if (keep2 == 1) {
				userType = userType | 0x00000200;
			}
			req.userInfo.UserType = userType;

		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			int userType = req.userInfo.UserType;
			// 机卡绑定标志
			if (req.机卡绑定 == 1) {
				userType = userType | 0x00000100;
			}
			req.userInfo.UserType = userType;
		} catch (Exception e) {
			// TODO: handle exception
		}
		// MsgHeader header =
	// wsy 20190514 统一对象缓存统一数据
	req.otherInfo.iconName = req.userInfo.UserIconName;
	req.userInfo.UserIconName = JSON.toJSONString(req.otherInfo);

		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.userInfo.UserName, CmdDefine.CMD_User_Add, req.userInfo, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userModify(String physicsNodeNumber, String loggicNodeNumber, UserInfo req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {

		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Modify, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userModify(String physicsNodeNumber, String loggicNodeNumber, MyUserInfoData req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		try {
			MyUser user = BaseDataManager.getInstance().userGet(req.userInfo.UserTelno);
			if (user == null) {
				System.out.println("用户有误");
			} else {
				int keep2 = Integer.valueOf(req.组播类型);
				int userType = req.userInfo.UserType;
				// 如果等于1的话, 将第二个字节的第二位附上keep2的值
				if (keep2 == 1) {
					userType = userType | 0x00000200;
				}
				// 机卡绑定标志
				if (req.机卡绑定 == 1) {
					userType = userType | 0x00000100;
				} else {
					;
				}
				req.userInfo.UserType = userType;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	// wsy 20190514 统一对象缓存统一数据
	req.userInfo.UserIconName = JSON.toJSONString(req.otherInfo);

		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.userInfo.UserName, CmdDefine.CMD_User_Modify, req.userInfo, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userDel(String physicsNodeNumber, String loggicNodeNumber, User req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Del, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userStopUserSvc(String physicsNodeNumber, String loggicNodeNumber, User req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Stop_Service, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userResumeUserSvc(String physicsNodeNumber, String loggicNodeNumber, User req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Resume_Service, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult userPasswordChange(String physicsNodeNumber, String loggicNodeNumber, User req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Change_Password, req, rsp, isAutoBusy, isWriteLog);
	}

	public SecuResult passwordChangeUser(String physicsNodeNumber, String loggicNodeNumber, User req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_Change_Password, req, rsp, isAutoBusy, isWriteLog);
	}

	/**
	 * 异步查询用户列表信息
	 */
	public int userQueryUserList_Async(String physicsNodeNumber, String logicNodeNumber) {
		// SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_User_Query_List;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, logicNodeNumber);

		// 设置用户查询超时时间增长
		int retCode = msgService.sendASyncMsg(header, new CommonRsp());

		return retCode;
	}

	public SecuResult userQueryUserList(String physicsNodeNumber, String loggicNodeNumber, UserQueryRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_User_Query_List;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		// 设置用户查询超时时间增长
		int retCode = msgService.sendSyncMsg(header, new CommonReq(), rsp, isAutoBusy, DEFAULT_TIMEOUT + 10 * 1000);

		if (retCode == 0) {
			result.addResult("", 0, cmdId, rsp.Result, isWriteLog);
		} else {
			result.addResult("", 0, cmdId, retCode, isWriteLog);
		}
		return result;
	}

	public SecuResult groupQueryGroupList(String physicsNodeNumber, String loggicNodeNumber, GroupQueryRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return groupQueryGroupList(physicsNodeNumber, loggicNodeNumber, rsp, isAutoBusy, isWriteLog, false);
	}

	public int groupQueryGroupList_Async(String physicsNodeNumber, String loggicNodeNumber) {
		// SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_Group_Query_List;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		// 设置用户查询超时时间增长
		int retCode = msgService.sendASyncMsg(header, new CommonReq());

		return retCode;
	}

	public int groupQueryGroupList_SubGroup_Async(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		Group req = new Group();
		req.GroupTelno = groupTelno;

		int cmdId = CmdDefine.CMD_Group_Query_List_SubGroup;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		// 设置用户查询超时时间增长
		int retCode = msgService.sendASyncMsg(header, req);

		return retCode;
	}

	public SecuResult groupQueryGroupList(String physicsNodeNumber, String loggicNodeNumber, GroupQueryRsp rsp, boolean isAutoBusy, boolean isWriteLog, boolean onlyOwnGroup) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_Group_Query_List;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		CommonReq req_value = new CommonReq();

		if (onlyOwnGroup) {
			cmdId = CmdDefine.CMD_Query_Group_List_By_OwnUser;
			req_value.UserName = LoginDataManager.getInstance().getName();
			req_value.UserTelno = LoginDataManager.getInstance().getUserTelphone();

			header.CmdID = cmdId;

		}

		int retCode = msgService.sendSyncMsg(header, req_value, rsp, isAutoBusy, DEFAULT_TIMEOUT);

		if (retCode == 0) {
	    result.addResult(loggicNodeNumber, 0, cmdId, rsp.Result, isWriteLog);
		} else {
	    result.addResult(loggicNodeNumber, 0, cmdId, retCode, isWriteLog);
		}
		return result;
	}

	public SecuResult dealGroupUserOneByOne(final MyGroup group, final ArrayList<String> addList, final ArrayList<String> delList) {

		final BaseListModel parentListModel = ((IBaseListModel) group.getParent()).getListModel();

		return dealGroupUserOneByOne(group, addList, delList, parentListModel);
	}

	public SecuResult dealGroupUserOneByOne(final MyGroup group, final ArrayList<String> addList, final ArrayList<String> delList, final BaseListModel parentListModel) {
		SecuResult result = new SecuResult();
		final String groupTel = group.getTelno();
		final String physicsNodeNumber = group.getPhysicsNodeNumber();

		for (String user : delList) {

			GroupUserProperty theProperty = new GroupUserProperty();
			theProperty.GroupTelno = groupTel;
			theProperty.UserTelno = user;
			theProperty.PttPriority = 7;
			theProperty.GroupPriority = 7;

			SecuResult result_ = dcsmain_comm_msg(physicsNodeNumber, group.getLogicNodeNumber(), theProperty.UserTelno + " <-- " + theProperty.GroupTelno, CmdDefine.CMD_Leave_Group, theProperty,
					new CommonRsp(), false, true);
			if (result_.getRetCode() == 0) {
			} else {
			}
		}

		for (String user : addList) {
			GroupUserProperty theProperty = new GroupUserProperty();
			theProperty.GroupTelno = groupTel;
			theProperty.UserTelno = user;
			theProperty.PttPriority = 7;
			theProperty.GroupPriority = 7;

			SecuResult result_ = dcsmain_comm_msg(physicsNodeNumber, group.getLogicNodeNumber(), theProperty.UserTelno + " -->" + theProperty.GroupTelno, CmdDefine.CMD_Join_Group, theProperty,
					new CommonRsp(), false, true);
			if (result_.getRetCode() == 0) {
			} else {

			}
		}
		return result;
	}

	/**
	 * 有可能后台异步线程出发,不触发进度条
	 * 
	 * @param group
	 * @param addList
	 * @param delList
	 * @param parentListModel
	 * @return
	 */
	public SecuResult dealGroupUserOneByOneByBack(final MyGroup group, final ArrayList<String> addList, final ArrayList<String> delList, final BaseListModel parentListModel) {
		SecuResult result = new SecuResult();

		final String groupTel = group.getTelno();
		final String physicsNodeNumber = group.getPhysicsNodeNumber();

		for (String addUser : delList) {
			GroupUserProperty theProperty = new GroupUserProperty();
			theProperty.GroupTelno = groupTel;
			theProperty.UserTelno = addUser;
			theProperty.PttPriority = 7;
			theProperty.GroupPriority = 7;

			SecuResult result_ = dcsmain_comm_msg(physicsNodeNumber, group.getLogicNodeNumber(), theProperty.UserTelno + " <-- " + theProperty.GroupTelno, CmdDefine.CMD_Leave_Group, theProperty,
					new CommonRsp(), false, true);

			result.addResult(theProperty.UserTelno + " <-- " + theProperty.GroupTelno, 0, CmdDefine.CMD_Leave_Group, result_.getRetCode(), false);
		}

		for (String addUser : addList) {
			GroupUserProperty theProperty = new GroupUserProperty();
			theProperty.GroupTelno = groupTel;
			theProperty.UserTelno = addUser;
			theProperty.PttPriority = 7;
			theProperty.GroupPriority = 7;

			SecuResult result_ = dcsmain_comm_msg(physicsNodeNumber, group.getLogicNodeNumber(), theProperty.UserTelno + " -->" + theProperty.GroupTelno, CmdDefine.CMD_Join_Group, theProperty,
					new CommonRsp(), false, true);

			result.addResult(theProperty.UserTelno + " -->" + theProperty.GroupTelno, 0, CmdDefine.CMD_Join_Group, result_.getRetCode(), false);

		}
		return result;
	}

	public SecuResult dealGroupUserOneByOneWithSubGroup(final MyGroup group, ArrayList<ModelBase> addList, ArrayList<ModelBase> delList, BaseListModel parentListModel) {
		SecuResult result = new SecuResult();

		final String groupTel = group.getTelno();
		final String physicsNodeNumber = group.getPhysicsNodeNumber();
		final String logicNodeNumber = group.getLogicNodeNumber();

		for (ModelBase user : delList) {
			if (user instanceof MyUser) {
				GroupUserProperty theProperty = new GroupUserProperty();
				theProperty.GroupTelno = groupTel;
				theProperty.UserTelno = ((MyUser) user).getIdlData().UserTelno;
				theProperty.PttPriority = 7;
				theProperty.GroupPriority = 7;

				SecuResult result_ = dcsmain_comm_msg(physicsNodeNumber, logicNodeNumber, theProperty.UserTelno + " <-- " + theProperty.GroupTelno, CmdDefine.CMD_Leave_Group, theProperty,
						new CommonRsp(), false, true);
			} else if (user instanceof MyGroup) {
				// 调用删除组方法
				MyGroup item = (MyGroup) user;
				Group req = item.getIdlData();

				req.ParGrpTelno = group.getIdlData().GroupTelno;
				SecuResult result_ = groupJoin_Del(physicsNodeNumber, logicNodeNumber, req, new CommonRsp(), true, true);
			}
		}

		for (ModelBase user : addList) {
			if (user instanceof MyUser) {

				GroupUserProperty theProperty = new GroupUserProperty();
				theProperty.GroupTelno = groupTel;
				theProperty.UserTelno = ((MyUser) user).getIdlData().UserTelno;
				theProperty.PttPriority = 7;
				theProperty.GroupPriority = 7;

				SecuResult result_ = dcsmain_comm_msg(physicsNodeNumber, group.getLogicNodeNumber(), theProperty.UserTelno + " -->" + theProperty.GroupTelno, CmdDefine.CMD_Join_Group, theProperty,
						new CommonRsp(), false, true);
			}

			else if (user instanceof MyGroup) {
				// 调用组方法
				MyGroup item = (MyGroup) user;
				Group req = item.getIdlData();

				req.ParGrpTelno = group.getIdlData().GroupTelno;

				if (!item.getLogicNodeNumber().equals(logicNodeNumber)) {
					// 属于异节点组
					req.GroupNodeFlag = 1;
					req.NodeNumber = item.getLogicNodeNumber();

					// 20180131 新分配的异节点组, 重新指定一个图标, 暂时使用临时组图标
					req.IconName = "O_Group_2_online.png";
				}

				CommonRsp rsp = new CommonRsp();

				SecuResult result_ = groupJoin_Add(physicsNodeNumber, logicNodeNumber, req, rsp, true, true);
			}
		}

		return result;
	}

	public void groupQueryGroupStatus(String physicsNodeNumber, String loggicNodeNumber) {
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Status, physicsNodeNumber, loggicNodeNumber);

		int retCode = msgService.sendASyncMsg(header, new CommonRsp());
	}

	public void groupQueryGroupStatus_Single(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		if (groupTelno.equals("")) {
			// System.out.println("查询组用户状态组号码为空.");
			return;
		}
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Status_Single, physicsNodeNumber, loggicNodeNumber);
		GroupUserStatusQueryReq req = new GroupUserStatusQueryReq();
		req.GroupTelno = groupTelno;
		int retCode = msgService.sendASyncMsg(header, req);
	}

	public void groupQueryGroup_Single(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		if (groupTelno.equals("")) {
			// System.out.println("查询组用户状态组号码为空.");
			return;
		}
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Single, physicsNodeNumber, loggicNodeNumber);
		GroupUserStatusQueryReq req = new GroupUserStatusQueryReq();
		req.GroupTelno = groupTelno;
		int retCode = msgService.sendASyncMsg(header, req);
	}

	public void queryGroupStatus_Single(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		if (groupTelno.equals("")) {
			// System.out.println("查询组用户状态组号码为空.");
			return;
		}
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Status_Single, physicsNodeNumber, loggicNodeNumber);
		GroupUserStatusQueryReq req = new GroupUserStatusQueryReq();
		req.GroupTelno = groupTelno;
		int retCode = msgService.sendASyncMsg(header, req);
	}

	public void queryGroupUserStatus(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		if (groupTelno.equals("")) {
			// System.out.println("查询组用户状态组号码为空.");
			return;
		}
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Mermber_Status, physicsNodeNumber, loggicNodeNumber);
		GroupUserStatusQueryReq req = new GroupUserStatusQueryReq();
		req.GroupTelno = groupTelno;

		int retCode = msgService.sendASyncMsg(header, req);

	}

	public void queryGroupUserStatus_Simple(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		if (groupTelno.equals("")) {
			// System.out.println("查询组用户状态组号码为空.");
			return;
		}

		// System.out.println("查询组用户状态组号码:." + groupTelno);

		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Mermber_Status_Simple, physicsNodeNumber, loggicNodeNumber);
		GroupUserStatusQueryReq req = new GroupUserStatusQueryReq();
		req.GroupTelno = groupTelno;

		int retCode = msgService.sendASyncMsg(header, req);

	}

	public void queryUserStatus(String physicsNodeNumber, String loggicNodeNumber, String userTelno) {
		if (userTelno.equals("")) {
			// System.out.println("查询组用户状态组号码为空.");
			return;
		}
		// System.out.println("查询组用户状态组号码:." + groupTelno);
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_User_Query_Status, physicsNodeNumber, loggicNodeNumber);
		GroupUserStatusQueryReq req = new GroupUserStatusQueryReq();
		req.GroupTelno = userTelno;

		int retCode = msgService.sendASyncMsg(header, req);

	}

	public SecuResult dealConfOperation(String physicsNodeNumber, String loggicNodeNumber, int cmdCode, CmdMsg req, CmdMsg rsp) {
		SecuResult result = new SecuResult();
		int cmdId = 5000;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_CONF, 5000, physicsNodeNumber, loggicNodeNumber);

		try {
			int retCode = msgService.sendSyncMsg(header, req, rsp, false, DEFAULT_TIMEOUT);

			if (retCode == 0) {
				result.addResult("", 0, cmdId, rsp.lErrorCode, false);
			} else {
				result.addResult("", 0, cmdId, retCode, false);
			}

		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		// TODO 自动生成方法存根

		return result;
	}

	public SecuResult queryUserInfo(String physicsNodeNumber, String logicNodeNumber, MyUser user, boolean isAutoBusy, boolean isWriteLog) {
		return queryUserInfo(physicsNodeNumber, logicNodeNumber, user, null, isAutoBusy, isWriteLog);
	}

	public SecuResult queryUserInfo(String physicsNodeNumber, String logicNodeNumber, MyUser user, BaseListModel parentListModel, boolean isAutoBusy, boolean isWriteLog) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_User_Query_Info;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, logicNodeNumber);
		User req = new User();
		req.GrpList = "";

		req.UserTelno = user.getIdlData().UserTelno;

		if (parentListModel != null) {
			if ((parentListModel.getStub() instanceof MyGroup)) {
				// 下发组信息,主要是为了查询上来的用户详细中附带用户ptt优先级
				req.GrpList = ((MyGroup) parentListModel.getStub()).getTelno();
			} else {
				req.GrpList = "";
			}
		}

		UserInfoQueryRsp rsp = new UserInfoQueryRsp();

		int retCode = msgService.sendSyncMsg(header, req, rsp, isAutoBusy, 5000);

		if (retCode == 0) {
			if (rsp.Result == 0) {
				if (user.getUserTelNo().equals("2080")) {
					System.out.println("2080 Physics_NodeNumber:" + physicsNodeNumber + ": logicNodeNumber :" + logicNodeNumber);
					System.out.println("");
				}

				// 伍尚源 20150625 作修正, 出现gps上报的用户与当前新建用户不一致
				// 伍尚源 20170209 作修正, 出现在用户查询时,子节点属性不正确,且可能组列表显示不正确
				// 伍尚源 20170419 作修正, 出现查询异节点查询时, 上报的header的节点与当前节点不一致
				user.setPhysicsNodeNumber(header.Physics_NodeNumber);
				user.setLogicNodeNumber(header.NodeNumber);
				user.setUserInfo(rsp.Info);
				ArrayList<MyGroup> list = user.getGroupList();

				for (MyGroup group : list) {
					group.getListModel().addUser(user);
				}
			}
			result.addResult(user.getUserName(), 0, cmdId, rsp.Result, isWriteLog);
		} else {
			result.addResult(user.getUserName(), 0, cmdId, retCode, isWriteLog);
		}
		return result;
	}

	public int queryUserInfo_Async(String physicsNodeNumber, String logicNodeNumber, MyUser user) {
		return queryUserInfo_Async(physicsNodeNumber, logicNodeNumber, user, null);
	}

	/**
	 * 异步查询用户详细信息
	 */
	public int queryUserInfo_Async(String physicsNodeNumber, String logicNodeNumber, MyUser user, BaseListModel parentListModel) {
		int cmdId = CmdDefine.CMD_User_Query_Info;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, logicNodeNumber);
		User req = user.getIdlData();

		req.GrpList = "";
		if (parentListModel != null) {
			if (!(parentListModel.getStub() instanceof MyGroup)) {
				// 下发组信息,主要是为了查询上来的用户详细中附带用户ptt优先级
				req.GrpList = ((MyGroup) parentListModel.getStub()).getTelno();
			} else {
				req.GrpList = "";
			}
		}

		int retCode = msgService.sendASyncMsg(header, req);

		return retCode;
	}

	/**
	 * 异步查询用户详细信息, 20160317 根据信息模糊查询用户列表, 返回节点内的所有用户信息,如果用户超过100个, 返回错误提示
	 */
	public int queryUserInfoListByDetail_Async(String physicsNodeNumber, String logicNodeNumber, MyUser user) {
		int cmdId = CmdDefine.CMD_User_Query_Info_detail;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, logicNodeNumber);
		User req = user.getIdlData();
		int retCode = msgService.sendASyncMsg(header, req);

		return retCode;
	}

	public SecuResult dealCreateGroupTmp(MyGroup group, GroupNuberReq req, GroupNuberRsp rsp) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CMD_Meeting_Group_Requst;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, group.getPhysicsNodeNumber(), group.getLogicNodeNumber());

		int retCode = msgService.sendSyncMsg(header, req, rsp, true, DEFAULT_TIMEOUT);

		if (retCode == 0) {
			result.addResult(group.getIdlData().GroupName, 0, cmdId, rsp.Result, true);
		} else {
			result.addResult(group.getIdlData().GroupName, 0, cmdId, retCode, true);
		}
		return result;
	}

	/**
	 * 组内用户查询
	 * 
	 * @param physicsNodeNumber
	 * @param loggicNodeNumber
	 * @param group
	 */
	public void queryGroupUserList(String physicsNodeNumber, String loggicNodeNumber, String group) {
		int cmdId = CmdDefine.CMD_Query_Group_User;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
		CommonReq req = new CommonReq();
		req.UserTelno = group;
		int retCode = msgService.sendASyncMsg(header, req);
	}

    /**
     * 组内用户查询
     * 
     * @param physicsNodeNumber
     * @param loggicNodeNumber
     */
    public void queryNodeUserList(String physicsNodeNumber, String loggicNodeNumber, String nodeNumber) {
	int cmdId = CmdDefine.CMD_Query_Group_User;
	MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
	CommonReq req = new CommonReq();
	req.UserName = nodeNumber;
	int retCode = msgService.sendASyncMsg(header, req);
    }

	/**
	 * 组内用户查询
	 * 
	 * @param physicsNodeNumber
	 * @param loggicNodeNumber
	 * @param group
	 */
	public void queryGroupUserList(String physicsNodeNumber, String loggicNodeNumber, MyGroup group, boolean sync) {

		int cmdId = CmdDefine.CMD_Query_Group_User;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
		CommonReq req = new CommonReq();
		req.UserTelno = group.getTelno();

		if (!sync) {
			int retCode = msgService.sendASyncMsg(header, req);
		} else {
			SecuResult result = new SecuResult();
			UserQueryRsp rsp = new UserQueryRsp();
			int retCode = msgService.sendSyncMsg(header, req, rsp, true, DEFAULT_TIMEOUT);

			if (retCode == 0) {
				// 将用户加入组内
				try {
		    // 处理用户所在的节点
		    MyNode dataNode = BaseDataManager.getInstance().nodeGet(header.NodeNumber);
					for (User user_get : rsp.UserList) {
						if (user_get.UserTelno.trim().equals("")) {
			    continue;
			}
			MyNode dataNode_current = dataNode;
			MyUser user = BaseDataManager.getInstance().userPushData(dataNode_current, user_get, false);
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}

		result.addResult(group.getIdlData().GroupName, 0, cmdId, rsp.Result, false);
	    } else {
		result.addResult(group.getIdlData().GroupName, 0, cmdId, retCode, false);
	    }
	}
    }

	public int queryGroupList_SubGroup_Async(String physicsNodeNumber, String loggicNodeNumber, String groupTelno) {
		Group holder = new Group();
		holder.GroupTelno = groupTelno;

		int cmdId = CmdDefine.CMD_Group_Query_List_SubGroup;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		// 设置用户查询超时时间增长
		int retCode = msgService.sendASyncMsg(header, holder);

		return retCode;
	}

	public void queryGroupStatus_TongJi(String physicsNodeNumber, String loggicNodeNumber) {
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_UserOnLine_UserAllSize, physicsNodeNumber, loggicNodeNumber);

		int retCode = msgService.sendASyncMsg(header, new CommonRsp());
	}

	public SecuResult stopUserBingDing(String physicsNodeNumber, String loggicNodeNumber, User req, CommonRsp rsp, boolean isAutoBusy, boolean isWriteLog) {
		return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, req.UserName, CmdDefine.CMD_User_机卡解除绑定, req, rsp, isAutoBusy, isWriteLog);
	}

	// add yzl 增加查询微信消息
	public SecuResult messageCodeQuery_WEIXIN_MESSAGE(String physicsNodeNumber, String loggicNodeNumber, MessageInfoQueryReq req, MessageInfoQueryRsp rsp) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.MESSAGECODE_CMD_CODE_QUERY_WEIXIN_MESSAGE;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);

		int retCode = msgService.sendSyncMsg(header, req, rsp, true, DEFAULT_TIMEOUT * 3);

		if (retCode == 0) {
			result.addResult("", 0, cmdId, rsp.Result, false);
		} else {
			result.addResult("", 0, cmdId, retCode, false);
		}

		return result;
	}

	public SecuResult messageCodeQuery_All_MESSAGE(String physicsNodeNumber, String loggicNodeNumber, MessageInfoQueryReq req, MessageInfoQueryRsp rsp) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.MESSAGECODE_CMD_CODE_QUERY_All_MESSAGE;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
		List<Streamable> rspList = new ArrayList<Streamable>();

		int retCode = msgService.sendSyncMsg(header, req, rsp, true, DEFAULT_TIMEOUT * 3);

		if (retCode == 0) {
			result.addResult("", 0, cmdId, rsp.Result, false);
		} else {
			result.addResult("", 0, cmdId, retCode, false);
		}

		return result;
	}

	public void queryNodeStatus(String physicsNodeNumber, String loggicNodeNumber) {
		if (true) {
			return;
		}
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_NODE_Query_Status, physicsNodeNumber, loggicNodeNumber);
		int retCode = msgService.sendASyncMsg(header, new CommonRsp());
	}

	public void queryGroupStatus(String physicsNodeNumber, String loggicNodeNumber) {
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, CmdDefine.CMD_Group_Query_Status, physicsNodeNumber, loggicNodeNumber);

		int retCode = msgService.sendASyncMsg(header, new CommonRsp());
	}

	public SecuResult callHistoryInfoQuery(String physicsNodeNumber, String loggicNodeNumber, CallHistoryInfoQueryReq req, CallHistoryInfoQueryRsp rsp) {
		SecuResult result = new SecuResult();
		int cmdId = CmdDefine.CALLHISTORY_CODE_CMD_CODE_QUERY_Idl;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
		List<Streamable> rspList = new ArrayList<Streamable>();

		int retCode = msgService.sendSyncMsg(header, req, rsp, true, DEFAULT_TIMEOUT * 3);

		if (retCode == 0) {
			result.addResult("", 0, cmdId, rsp.Result, false);
		} else {
			result.addResult("", 0, cmdId, retCode, false);
		}

		return result;
    }
    public SecuResult messageCodeEdit(String physicsNodeNumber, String loggicNodeNumber, MessageOperationReq req, CommonRsp rsp) {
	return dcsmain_comm_msg(physicsNodeNumber, loggicNodeNumber, "短信", CmdDefine.MESSAGECODE_CMD_CODE_EDIT, req, rsp, false, true);
    }

	public int 查询国标设备列表() {
		int cmdId = CmdDefine.CMD_Query_国标设备;
		MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, null, null);
		int retCode = msgService.sendASyncMsg(header, new CommonRsp());
		return retCode;
	}
    /**
     * 发送通用的json请求命令道后台, 并只能异步接收返回结果
     * @param physicsNodeNumber
     * @param loggicNodeNumber
     * @param cmdId
     * @param reqJson
     * @return
     */
    public SecuResult jSonCommMessage(String physicsNodeNumber, String loggicNodeNumber, int cmdId, Object reqJson) {
	SecuResult result = new SecuResult();
	MsgHeader header = msgService.getMessageHeader(Constant.MODULE_ID_DCSMAIN, cmdId, physicsNodeNumber, loggicNodeNumber);
	List<Streamable> rspList = new ArrayList<Streamable>();
	CommonJsonContentReq req = new CommonJsonContentReq();
	req.jsonContent = JSON.toJSONString(reqJson);
	int retCode = msgService.sendASyncMsg(header, req);
	return result;
    }

}
